package com.xjlsoft.weather.service.impl;


import com.xjlsoft.weather.common.util.DateUtil;
import com.xjlsoft.weather.dto.DataAvgDto;
import com.xjlsoft.weather.dto.ExportDto;
import com.xjlsoft.weather.dto.GroundDataListDto;
import com.xjlsoft.weather.dto.WeatherDataQueryDto;
import com.xjlsoft.weather.entity.GroundAvg;
import com.xjlsoft.weather.entity.GroundData;
import com.xjlsoft.weather.entity.GroundDataExcelDto;
import com.xjlsoft.weather.enums.DataSourceEnum;
import com.xjlsoft.weather.enums.DataTypeEnum;
import com.xjlsoft.weather.mapper.GroundAvgMapper;
import com.xjlsoft.weather.mapper.GroundDataMapper;
import com.xjlsoft.weather.service.GroundDataService;
import com.xjlsoft.weather.util.WeatherDataUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 地面数据 服务实现类
 * </p>
 *
 * @author zhangbanglong
 * @since 2020-07-22
 */
@Service
public class GroundDataServiceImpl extends ServiceImpl<GroundDataMapper, GroundData> implements GroundDataService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(GroundDataServiceImpl.class);

	@Value("${fileSaveDirPrefix}")
	private String fileSaveDirPrefix;// 文件存储根路径
	
	
	@Autowired
	private GroundDataMapper groundDataMaper;
	
	@Autowired
	private GroundAvgMapper goundAvgMapper;

	@Override
	public boolean saveList(List<GroundData> listGrounData) {
		boolean result = this.saveBatch(listGrounData);
		return result;
	}

	@Override
	public GroundData getGroundDataByid(String groundDataId) {
		GroundData  gd=groundDataMaper.selectById(groundDataId);
		return gd;
	}

	@Override
	public IPage<GroundDataListDto> getList(Page page, WeatherDataQueryDto weatherDataQueryDto) throws Exception {
		QueryWrapper<GroundData> query = Wrappers.query();
        //拼接条件
        /*query.eq(StringUtils.isNotBlank(weatherDataDto.getMaleFlag()), "male_flag", personQueryParam.getMaleFlag())
                .eq(ObjectUtil.isNotNull(personQueryParam.getNationType()), "nation_type", personQueryParam.getNationType())
                .eq(ObjectUtil.isNotNull(personQueryParam.getEducationType()), "education_type", personQueryParam.getEducationType())
                .eq(ObjectUtil.isNotNull(personQueryParam.getPoliticsType()), "politics_type", personQueryParam.getPoliticsType()));*/
    
		if(StringUtils.isNotBlank(weatherDataQueryDto.getDateStart())){
			Timestamp timestart=WeatherDataUtil.strToTimestamp(weatherDataQueryDto.getDateStart());
			query.ge("data_time", timestart);
		}
		if(StringUtils.isNotBlank(weatherDataQueryDto.getDateEnd())){
			Timestamp timeend=WeatherDataUtil.strToTimestamp(weatherDataQueryDto.getDateEnd());
			query.le("data_time", timeend);
		}
        IPage<GroundDataListDto> list =groundDataMaper.getGroundDatasByParams(page, query);
        		
        		
		return list;
	}

	@Override
	public List<GroundData> getList(ExportDto exportDto) throws Exception {
		QueryWrapper<GroundData> query = Wrappers.query();
		//query.select(weatherDataDto.getSelectFilds());
		/*query.select("ground_data_id","data_query_id","create_time","data_time","station_name","province","city",
				    "cnty","town","year","mon","day","hour","lat","lon","alti","prs_sensor_alti","win_s_sensor_heigh","prs",
				     "prs_sea","tem","rhu","pre","gst");*/
        //拼接条件
        /*query.eq(StringUtils.isNotBlank(weatherDataDto.getMaleFlag()), "male_flag", personQueryParam.getMaleFlag())
                .eq(ObjectUtil.isNotNull(personQueryParam.getNationType()), "nation_type", personQueryParam.getNationType())
                .eq(ObjectUtil.isNotNull(personQueryParam.getEducationType()), "education_type", personQueryParam.getEducationType())
                .eq(ObjectUtil.isNotNull(personQueryParam.getPoliticsType()), "politics_type", personQueryParam.getPoliticsType()));*/
    
		if(StringUtils.isNotBlank(exportDto.getDateStart())){
			Timestamp timestart=WeatherDataUtil.strToTimestamp(exportDto.getDateStart());
			query.ge("data_time", timestart);
		}
		if(StringUtils.isNotBlank(exportDto.getDateEnd())){
			Timestamp timeend=WeatherDataUtil.strToTimestamp(exportDto.getDateEnd());
			query.le("data_time", timeend);
		}
       
        List<GroundData> list =groundDataMaper.selectList(query);
        		//groundDataMaper.selectPage(page, query);
		return list;
	}

	@Override
	public List<Map<String, Object>> getListMap(ExportDto exportDto) throws Exception {
		QueryWrapper<GroundData> query = Wrappers.query();
		query.select(exportDto.getSelectFilds());
	
		
		if(StringUtils.isNotBlank(exportDto.getDateStart())){
			Timestamp timestart=WeatherDataUtil.strToTimestamp(exportDto.getDateStart());
			query.ge("data_time", timestart);
		}
		if(StringUtils.isNotBlank(exportDto.getDateEnd())){
			Timestamp timeend=WeatherDataUtil.strToTimestamp(exportDto.getDateEnd());
			query.le("data_time", timeend);
		}
		 List<Map<String, Object>> map= groundDataMaper.selectMaps(query);	
	     return map;
	}

	
	
	@Override
	public int getListMapCount(ExportDto exportDto) throws Exception {
		QueryWrapper<GroundData> query = Wrappers.query();
		if(StringUtils.isNotBlank(exportDto.getDateStart())){
			Timestamp timestart=WeatherDataUtil.strToTimestamp(exportDto.getDateStart());
			query.ge("data_time", timestart);
		}
		if(StringUtils.isNotBlank(exportDto.getDateEnd())){
			Timestamp timeend=WeatherDataUtil.strToTimestamp(exportDto.getDateEnd());
			query.le("data_time", timeend);
		}
		int count=groundDataMaper.selectCount(query);
		return count;
	}

	@Override
	public Map<String, Object> getCurrentYear2WindsAnd10WinD() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		//风速 查询
		QueryWrapper<GroundData> queryWinS = Wrappers.query();
		
		
		//String year=DateUtil.DateToString(newDate, "yyyy");
		
		String year="2011";
		queryWinS.select("win_s_avg_2mi");
		queryWinS.ge("win_s_avg_2mi", 0.0);
		queryWinS.ge("win_d_avg_10mi", 0.0);
		queryWinS.eq("year",year);
		queryWinS.orderByAsc("data_time");
		List<Object> list1 = groundDataMaper.selectObjs(queryWinS);
		map.put("win_s", list1);  //2分钟平均风速
		
		
		//风速 查询
		QueryWrapper<GroundData> queryWinD = Wrappers.query();
				
				
				
		queryWinD.select("win_d_avg_10mi");
		queryWinD.eq("year",year);
		queryWinD.ge("win_s_avg_2mi", 0.0);
		queryWinD.ge("win_d_avg_10mi", 0.0);
		queryWinD.orderByAsc("data_time");
		
		List<Object> list2 = groundDataMaper.selectObjs(queryWinD);
		map.put("win_d", list2);  //2分钟平均风速
		
		
		//风速 查询
		QueryWrapper<GroundData> queryTimes = Wrappers.query();
						
						
						
		queryTimes.select("DATE_FORMAT(data_time,'%Y-%m-%d %H')");
		queryTimes.eq("year",year);
		queryTimes.ge("win_s_avg_2mi", 0.0);
		queryTimes.ge("win_d_avg_10mi", 0.0);
		queryTimes.orderByAsc("data_time");
		List<Object> times = groundDataMaper.selectObjs(queryTimes);
		map.put("times", times); 
		
		return map;
		
	}

	@Override
	public Map<String, Object> getCurrentDayWeekMon2WindsAnd10WinD(String dataType) throws Exception {
		
		Map<String, Object> map = new HashMap<String, Object>();
		List<String > list=groundDataMaper.getMaxDataTime2();
		
		if(list ==null ){
			map.put("success", false);
			map.put("message", "没有查询到最新的 原始日数据时间(groundData- data_time)");
			return map;
		}else{
			if(list.size() ==0 ){
				map.put("success", false);
				map.put("message", "没有查询到最新的 原始日数据时间(groundData- data_time)");
				return map;
			}
		}
		
		Date maxDate=DateUtil.StringToDate(list.get(0), "yyyy-MM-dd HH:mm:ss");
		
		if(DataTypeEnum.Day1.getValue().equals(dataType)){
			
			Map<String,String> mp=WeatherDataUtil.getYearMonDayStr(maxDate);
			//风速 查询
			QueryWrapper<GroundData> queryWinS = Wrappers.query();
			queryWinS.select("win_s_avg_2mi");
			queryWinS.gt("win_s_avg_2mi", 0.0);
			queryWinS.gt("win_d_avg_10mi", 0.0);
			
			queryWinS.ne("win_s_avg_2mi", 999999.0000);
			queryWinS.ne("win_d_avg_10mi", 999999.0000);
			queryWinS.ne("win_s_avg_2mi", 999998.0000);
			queryWinS.ne("win_d_avg_10mi", 999998.0000);
			
			queryWinS.eq("year", mp.get("year"));
			queryWinS.eq("mon", mp.get("mon"));
			queryWinS.eq("day", mp.get("day"));
			queryWinS.orderByAsc("data_time");
			List<Object> list1 = groundDataMaper.selectObjs(queryWinS);
			map.put("win_s", list1);  //2分钟平均风速
			
			
			//风速 查询
			QueryWrapper<GroundData> queryWinD = Wrappers.query();
			queryWinD.select("win_d_avg_10mi");
			queryWinD.ge("data_time",maxDate);
			queryWinD.gt("win_s_avg_2mi", 0.0);
			queryWinD.gt("win_d_avg_10mi", 0.0);
			queryWinD.ne("win_s_avg_2mi", 999999.0000);
			queryWinD.ne("win_d_avg_10mi", 999999.0000);
			queryWinD.ne("win_s_avg_2mi", 999998.0000);
			queryWinD.ne("win_d_avg_10mi",999998.0000);
			queryWinD.eq("year", mp.get("year"));
			queryWinD.eq("mon", mp.get("mon"));
			queryWinD.eq("day", mp.get("day"));
			queryWinD.orderByAsc("data_time");
			
			List<Object> list2 = groundDataMaper.selectObjs(queryWinD);
			map.put("win_d", list2);  //2分钟平均风速
			
			
			//风速 查询
			QueryWrapper<GroundData> queryTimes = Wrappers.query();
			queryTimes.select("DATE_FORMAT(data_time,'%Y-%m-%d %H')");
			queryTimes.ge("data_time",maxDate);
			queryTimes.gt("win_s_avg_2mi", 0.0);
			queryTimes.gt("win_d_avg_10mi", 0.0);
			queryTimes.ne("win_s_avg_2mi", 999999.0000);
			queryTimes.ne("win_d_avg_10mi", 999999.0000);
			queryTimes.ne("win_s_avg_2mi", 999998.0000);
			queryTimes.ne("win_d_avg_10mi", 999998.0000);
			queryTimes.eq("year", mp.get("year"));
			queryTimes.eq("mon", mp.get("mon"));
			queryTimes.eq("day", mp.get("day"));
			queryTimes.orderByAsc("data_time");
			List<Object> times = groundDataMaper.selectObjs(queryTimes);
			map.put("times", times); 
			
			
			//风速 查询
			QueryWrapper<GroundData> query16 = Wrappers.query();
			query16.select("getFangxiangStr(win_d_avg_10mi)");
			query16.gt("win_s_avg_2mi", 0.0);
			query16.gt("win_d_avg_10mi", 0.0);
			query16.eq("year", mp.get("year"));
			query16.eq("mon", mp.get("mon"));
			query16.eq("day", mp.get("day"));
			query16.ge("data_time",maxDate);
			query16.orderByAsc("data_time");
			List<Object> winD16 = groundDataMaper.selectObjs(query16);
			map.put("wind_d_16", winD16); 
			
			map.put("success", true);
			
			return map;

		}else if(DataTypeEnum.Day7.getValue().equals(dataType)){
			//风速 查询
			QueryWrapper<GroundAvg> queryWinS = Wrappers.query();
			queryWinS.select("win_s_avg_2mi");
			queryWinS.eq("data_type", "day");
			queryWinS.le("date_time",maxDate);
			queryWinS.orderByAsc("date_time");
			queryWinS.last("limit 0,7");
			List<Object> list1 = goundAvgMapper.selectObjs(queryWinS);
			map.put("win_s", list1);  //2分钟平均风速
			
			
			//风速 查询
			QueryWrapper<GroundAvg> queryWinD = Wrappers.query();
			queryWinD.select("win_d_avg_10mi");
			queryWinD.le("date_time",maxDate);
			queryWinD.eq("data_type", "day");
			queryWinD.orderByAsc("date_time");
			queryWinD.last("limit 0,7");
			List<Object> list2 = goundAvgMapper.selectObjs(queryWinD);
			map.put("win_d", list2);  //2分钟平均风速
			
			
			//风速 查询
			QueryWrapper<GroundAvg> queryTimes = Wrappers.query();
			queryTimes.select("DATE_FORMAT(date_time,'%Y-%m-%d')");
			queryTimes.le("date_time",maxDate);
			queryTimes.eq("data_type", "day");
			queryTimes.orderByAsc("date_time");
			queryTimes.last("limit 0,7");
			List<Object> times = goundAvgMapper.selectObjs(queryTimes);
			map.put("times", times); 
			
			
			//风速 查询
			QueryWrapper<GroundAvg> query16 = Wrappers.query();
			query16.select("getFangxiangStr(win_d_avg_10mi)");
			query16.eq("data_type", "day");
			query16.le("date_time",maxDate);
			query16.orderByAsc("date_time");
			query16.last("limit 0,7");
			List<Object> winD16 = goundAvgMapper.selectObjs(query16);
			map.put("wind_d_16", winD16); 
			map.put("success", true);
			
			return map;
		}else if(DataTypeEnum.Day30.getValue().equals(dataType)){
			//风速 查询
			QueryWrapper<GroundAvg> queryWinS = Wrappers.query();
			queryWinS.select("win_s_avg_2mi");
			queryWinS.eq("data_type", "day");
			queryWinS.le("date_time",maxDate);
			queryWinS.orderByAsc("date_time");
			queryWinS.last("limit 0,30");
			List<Object> list1 = goundAvgMapper.selectObjs(queryWinS);
			map.put("win_s", list1);  //2分钟平均风速
			
			
			//风速 查询
			QueryWrapper<GroundAvg> queryWinD = Wrappers.query();
			queryWinD.select("win_d_avg_10mi");
			queryWinD.eq("data_type", "day");
			queryWinD.le("date_time",maxDate);
			queryWinD.orderByAsc("date_time");
			queryWinD.last("limit 0,30");
			List<Object> list2 = goundAvgMapper.selectObjs(queryWinD);
			map.put("win_d", list2);  //2分钟平均风速
			
			
			//风速 查询
			QueryWrapper<GroundAvg> queryTimes = Wrappers.query();
			queryTimes.select("DATE_FORMAT(date_time,'%Y-%m-%d')");
			queryTimes.le("date_time",maxDate);
			queryTimes.eq("data_type", "day");
			queryTimes.orderByAsc("date_time");
			queryTimes.last("limit 0,30");
			List<Object> times = goundAvgMapper.selectObjs(queryTimes);
			map.put("times", times); 
			
			
			//风速 查询
			QueryWrapper<GroundAvg> query16 = Wrappers.query();
			query16.select("getFangxiangStr(win_d_avg_10mi)");
			query16.eq("data_type", "day");
			query16.le("date_time",maxDate);
			query16.orderByAsc("date_time");
			query16.last("limit 0,30");
			List<Object> winD16 = goundAvgMapper.selectObjs(query16);
			map.put("wind_d_16", winD16); 
			map.put("success", true);
			
			return map;
		}else{
			map.put("success", false);
			map.put("message", "后台没有接收到,查询数据类型参数");
			return map;
		}
		
		
	}

	

}
